package cn.edu.nhic.order.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.util.List;

import cn.edu.nhic.order.R;
import cn.edu.nhic.order.ShopDetailActivity;
import cn.edu.nhic.order.bean.ShopBean;

/**
 * 店铺列表适配器
 */
public class ShopAdapter extends BaseAdapter {
    private Context context;   //上下文
    private int resource;      //自定义布局
    private List<ShopBean> dataList; //数据源

    public ShopAdapter(Context context, int resource, List<ShopBean> dataList) {
        this.context = context;
        this.resource = resource;
        this.dataList = dataList;
    }
    //注意很重要 获取数据并更新界面
    public void setData(List<ShopBean> dataList){
        this.dataList=dataList;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return this.dataList.size();
    }

    @Override
    public Object getItem(int i) {
        return this.dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final HoldView hv;
        if(view==null){
            hv=new HoldView();
            //布局填充器
            LayoutInflater layoutInflater=LayoutInflater.from(this.context);
            view=layoutInflater.inflate(this.resource,null);
            hv.tv_shop_name=view.findViewById(R.id.tv_shopName);
            hv.tv_sale_num=view.findViewById(R.id.tv_saleNum);
            hv.tv_cost=view.findViewById(R.id.tv_cost);
            hv.tv_feature=view.findViewById(R.id.tv_feature);
            hv.tv_time=view.findViewById(R.id.tv_time);
            hv.iv_shop_pic=view.findViewById(R.id.iv_shopPic);
            view.setTag(hv);
        }else{
            hv=(HoldView) view.getTag();
        }
        //当前条目的JavaBean信息
        final ShopBean sb=this.dataList.get(i);
        hv.tv_shop_name.setText(sb.getShopName());
        hv.tv_cost.setText("起送"+sb.getOfferPrice()+"|配送"+sb.getDistributionCost());
        hv.tv_sale_num.setText("月售"+sb.getSaleNum());
        hv.tv_feature.setText(sb.getFeature());
        hv.tv_time.setText(sb.getTime());
        //加载图片Glide.with(上下文).load(图片资源).into(需要加载的目标图片组件)
        Glide.with(this.context)
                .load(sb.getShopPic())
                .into(hv.iv_shop_pic);
        //view事件
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,"测试",Toast.LENGTH_LONG).show();
                //定义跳转目标
                Intent intent=new Intent(context, ShopDetailActivity.class);
                //店铺列表shopbean传递到店铺详情页
                intent.putExtra("shopBean",sb);
                context.startActivity(intent);
            }
        });
        return view;
    }
    class HoldView {
        TextView tv_shop_name, tv_sale_num,tv_cost,tv_feature,tv_time;
        ImageView iv_shop_pic;
    }

}
